<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="applyNickName">
        <el-input v-model="queryParams.applyNickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="applyName">
        <el-input v-model="queryParams.applyName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="applyPhone">
        <el-input v-model="queryParams.applyPhone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['city:apply:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['city:apply:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['city:apply:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['city:apply:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
   <!--   <el-table-column label="合作申请ID" align="center" prop="applyId" width="180" /> -->
      <el-table-column label="昵称" align="center" prop="applyNickName" width="180" />
      <el-table-column label="姓名" align="center" prop="applyName" width="180" />
      <el-table-column label="手机号" align="center" prop="applyPhone" width="110" />
      <el-table-column label="行业类型" align="center" prop="applyTrade">
        <template slot-scope="scope">
          <el-popover title="行业类型" placement="top-start" width="400" trigger="click">
            <el-input disabled type="textarea" maxlength="200" show-word-limit :autosize="{ minRows: 5, maxRows: 10}"
              v-model="scope.row.applyTrade" placeholder="行业类型"></el-input>
            <el-button round size="mini" slot="reference" icon="el-icon-search" type="text" plain>查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="其他" align="center" prop="applyOther">
        <template slot-scope="scope">
          <el-popover title="其他" placement="top-start" width="400" trigger="click">
            <el-input disabled type="textarea" maxlength="200" show-word-limit :autosize="{ minRows: 5, maxRows: 10}"
              v-model="scope.row.applyOther" placeholder="其他"></el-input>
            <el-button round size="mini" slot="reference" icon="el-icon-search" type="text" plain>查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="备注" align="center" prop="applyRemark">
        <template slot-scope="scope">
          <el-popover title="备注" placement="top-start" width="400" trigger="click">
            <el-input disabled type="textarea" maxlength="200" show-word-limit :autosize="{ minRows: 5, maxRows: 10}"
              v-model="scope.row.applyRemark" placeholder="备注"></el-input>
            <el-button round size="mini" slot="reference" icon="el-icon-search" type="text" plain>查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['city:apply:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['city:apply:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改合作申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body center>
      <el-form class="demo" label-position="top" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="applyNickName">
          <el-input v-model="form.applyNickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="姓名" prop="applyName">
          <el-input v-model="form.applyName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="applyPhone">
          <el-input v-model="form.applyPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="行业类型" prop="applyTrade">
          <el-cascader style="width: 80%;" v-model="value" :options="options" :props="props" collapse-tags
            clearable></el-cascader>
        </el-form-item>
        <el-form-item label="其他" prop="applyOther">
          <el-cascader style="width: 80%;" v-model="value2" :options="options" :props="props" collapse-tags
            clearable></el-cascader>
        </el-form-item>
        <el-form-item label="备注" prop="applyRemark">
          <el-input v-model="form.applyRemark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listApply,
    getApply,
    delApply,
    addApply,
    updateApply
  } from "@/api/city/apply";
  import {
    tradeOptions
  } from "@/api/city/trade";
  export default {
    name: "Apply",
    data() {
      return {
        value: null,
        value2: null,
        props: {
          multiple: true
        },
        options: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 合作申请表格数据
        applyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          applyNickName: null,
          applyName: null,
          applyPhone: null,
          applyTrade: null,
          applyOther: null,
          applyRemark: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getTradeOptions();
    },
    methods: {
      /** 查询合作申请列表 */
      getList() {
        this.loading = true;
        listApply(this.queryParams).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询行业级联选择 */
      getTradeOptions() {
        tradeOptions({}).then(response => {
          this.options = response;
          console.log("options:", this.options)
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          applyId: null,
          applyNickName: null,
          applyName: null,
          applyPhone: null,
          applyTrade: null,
          applyOther: null,
          applyRemark: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.applyId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加合作申请";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const applyId = row.applyId || this.ids
        getApply(applyId).then(response => {
          this.form = response.data;
          this.value = JSON.parse(response.data.applyTrade);
          this.value2 = JSON.parse(response.data.applyOther);
          this.open = true;
          this.title = "修改合作申请";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.form.applyTrade = JSON.stringify(this.value)
        this.form.applyOther = JSON.stringify(this.value2)
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.applyId != null) {
              updateApply(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addApply(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const applyIds = row.applyId || this.ids;
        this.$modal.confirm('是否确认删除合作申请编号为"' + applyIds + '"的数据项？').then(function() {
          return delApply(applyIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('/system/city/apply/export', {
          ...this.queryParams
        }, `apply_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>


<style>
  .demo::-webkit-scrollbar {

    display: none;
    /* Chrome Safari */

  }



  .demo {

    scrollbar-width: none;
    /* firefox */

    -ms-overflow-style: none;
    /* IE 10+ */

    overflow-x: hidden;

    overflow-y: auto;
    height: 400px;
  }
</style>
